
<html>

  <head>

    <title>Chile 2010 Earthquake - Overview of Tectonic Activity</title>

    <style>
      body, table {
        font-family: Tahoma;
        font-size: 11px;
        padding:0px;
        margin:0px;
      }
      a:link                { color:#0000ff; text-decoration:none;      border:0px solid #000000; }
      a:visited             { color:#000000; text-decoration:none;      border:0px solid #000000; }
      a:hover               { color:#0000ff; text-decoration:underline; border:0px solid #000000; }
      a:active              { color:#0000ff; text-decoration:none;      border:0px solid #000000; }
      a.black:link          { color:#cccccc; text-decoration:none;      border:0px solid #000000; }
      a.black:visited       { color:#cccccc; text-decoration:none;      border:0px solid #000000; }
      a.black:hover         { color:#0000ff; text-decoration:underline; border:0px solid #000000; }
      a.black:active        { color:#cccccc; text-decoration:none;      border:0px solid #000000; }
      tr.item:hover         { background:SkyBlue; }
      
    </style>
    
<?php

  //=======================================================================
  // ___fetch
  //=======================================================================
  
  function ___fetch($url) {
    $___curl = curl_init();
    curl_setopt($___curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($___curl, CURLOPT_URL, $url);
    curl_setopt($___curl, CURLOPT_RETURNTRANSFER, true);
    $page = curl_exec($___curl);
    curl_close($___curl);
    return $page;
  }
  
  function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {
    $theta = $longitude1 - $longitude2;
    $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
    $distance = acos($distance);
    $distance = rad2deg($distance);
    $distance = $distance * 60 * 1.1515 * 1.609344;
    return (round($distance));
  }

  $timeline = "hidden";
  if ($_GET['timeline'] == 'true')
    $timeline = "visible";
  $map = "hidden";
  if ($_GET['map'] == 'true')
    $map = "visible";
  
  $list = ___fetch("http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt");
 
  $lines = explode("\n", $list);
  unset($lines[0]);
  
  $dataset  = array();
  $index    = 0;
  
  $min_magn = 100.0;
  $max_magn = 0;
  $min_dist = 500.0;
  $max_dist = 500.0;
  $min_unix = strtotime("Saturday, February 27, 2010 00:00:00 UTC");
  $max_unix = time();
  
  $stgo_lat = -33.45;
  $stgo_lon = -70.666667;
  
  foreach ($lines as $line) {
    
    $segments   = preg_split ('/,"|",|"/m', $line);
    $catalog    = explode(",", $segments[0]);
    $geological = explode(",", $segments[2]);
    
    $timestamp  = $segments[1];
    $region     = $segments[3];
    
    if (strstr($region, 'Chile') || strstr($region, 'Argentina') || strstr($region, 'Ecuador') || strstr($region, 'Peru')) {
      
      $unix = strtotime($timestamp);
      if ($unix > $min_unix) {
        
        $dataset[$index]['Src']       = $catalog[0];
        $dataset[$index]['Eqid']      = $catalog[1];
        $dataset[$index]['Version']   = $catalog[2];
        $dataset[$index]['Datetime']  = $timestamp;
        $dataset[$index]['Lat']       = $geological[0];
        $dataset[$index]['Lon']       = $geological[1];
        $dataset[$index]['Magnitude'] = $geological[2];
        $dataset[$index]['Depth']     = $geological[3];
        $dataset[$index]['NST']       = $geological[4];
        $dataset[$index]['Region']    = $region;
        $dataset[$index]['Unix']      = $unix;
        
        $dataset[$index]['Distance']  = getDistanceBetweenPointsNew(
          $dataset[$index]['Lat'],
          $dataset[$index]['Lon'],
          $stgo_lat,
          $stgo_lon
        );
        
        if ($min_dist > $dataset[$index]['Distance'])
          $min_dist = $dataset[$index]['Distance'];
        
        //if ($max_dist < $dataset[$index]['Distance'])
        //  $max_dist = $dataset[$index]['Distance'];
        
        if ($min_magn > $dataset[$index]['Magnitude'])
          $min_magn = $dataset[$index]['Magnitude'];
          
        if ($max_magn < $dataset[$index]['Magnitude'])
          $max_magn = $dataset[$index]['Magnitude'];
        
        $index++;
      }
    }
  }

  $counter  = 1;
  
  echo "<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAz2IwJLU6R-R9CdY06Qd6uxSG7qkYMwcfr7IPwWIousOwMpFOEBRJIf_YBzlLhIv4Dqhi6SYyBOE-KQ' type='text/javascript'></script>";
  echo "<script type='text/javascript'>\n";
  echo "  var origin;\n";
  echo "  function load() {\n";
  echo "    if (GBrowserIsCompatible()) {\n";
  echo "      var map = new GMap2(document.getElementById(\"map\"));\n";
  echo "      map.addControl(new GSmallMapControl());\n";
	echo "      map.addControl(new GMapTypeControl());\n";
  echo "      map.setCenter(new GLatLng($stgo_lat-2, $stgo_lon-2), 4);\n";
  echo "      var stgo = new GMarker(new GLatLng($stgo_lat, $stgo_lon));\n";
  echo "      map.addOverlay(stgo);";
  echo "      origin = new GMarker(new GLatLng($stgo_lat, $stgo_lon));\n";
  echo "      map.addOverlay(origin);";
  echo "    }\n";
  echo "  }\n";
  echo "  function setOrigin(lat, lon) {\n";
  echo "    origin.setLatLng(new GLatLng(lat, lon));\n";
  echo "  }\n";
  echo "</script>\n";
  echo "</head>\n";

  
  echo "<body onload='load()' onunload='GUnload()'>\n";
  
  $fullpage = true;
  echo "<table style='min-width:890px;width:890px;'>\n";
  if ($timeline == 'visible' || $map == 'visible') {
    echo "  <tr>\n";
    if ($map == 'visible') {
      $fullpage = false;
      echo "    <td><div id='map' style='height:300px;width:400px;'></div></td>\n";
    }
    if ($timeline == 'visible')
      echo "    <td><div id='chart_div_chile' style='height:300px;width:490px;'></div></td>\n";
    echo "  </tr>\n";
  }
  
  echo "  <tr>\n";
  echo "    <td colspan='2'>\n";
  if ($fullpage == false)
    echo "      <div style='height:250px;overflow-y:auto;'>\n";
  
  $shakes      = sizeof($dataset);
  $first_unix  = $dataset[$shakes-1]['Unix'];
  $avg_freq    = round($shakes / (($max_unix - $first_unix)/60.0/60.0),2);
  
  echo "<table cellpadding='3' cellspacing='0'>";
  
  echo "<tr>";
  echo "  <td colspan='20'>";
  echo "    <a class='black' href='index.php?timeline=false&map=false'>timeline:hide map:hide</a>&nbsp;&nbsp;&nbsp;&nbsp;";
  echo "    <a class='black' href='index.php?timeline=false&map=true'>timeline:hide map:show</a>&nbsp;&nbsp;&nbsp;&nbsp;";
  echo "    <a class='black' href='index.php?timeline=true&map=false'>timeline:show map:hide</a>&nbsp;&nbsp;&nbsp;&nbsp;";
  echo "    <a class='black' href='index.php?timeline=true&map=true'>timeline:show map:show</a> ";
  echo "  </td>";
  echo "</tr>";
  echo "<tr>";
  echo "  <td colspan='20'>";
  echo "    min: $min_magn&nbsp;|&nbsp;max: $max_magn&nbsp;|&nbsp;";
  echo "    count: $shakes&nbsp;|&nbsp;frequency: $avg_freq per hour&nbsp;|&nbsp;";
  echo "    source: <a target='_blank' href='http://earthquake.usgs.gov/earthquakes/recenteqsww/Maps/region/S_America_eqs.php'>USGS</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  echo "    <span style='color:#888888'>".date("D d. M, H:i:s", $max_unix-(4*60*60))."(.cl)</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  echo "  </td>";
  echo "</tr>";
  
  foreach($dataset as $item) {
    
    $magn_color = round(255.0 - ((float)$item['Magnitude'] - (float)$min_magn) * (255.0 / ((float)$max_magn - (float)$min_magn)));
    //$dist_color = round(126.0+((float)$item['Distance'] - (float)$min_dist) * (255.0 / ((float)$max_dist - (float)$min_dist))/2.0);
    //$dist_color = round(pow((((float)$item['Distance'] - (float)$min_dist) * (255.0 / ((float)$max_dist - (float)$min_dist)))/15.9687194, 2));
    //$dist_color = round(((float)$item['Distance'] - (float)$min_dist) * (255.0 / ((float)$max_dist - (float)$min_dist)));
    
    $dist_color = round(255*(((float)$item['Distance'] - (float)$min_dist) / ((float)$max_dist - (float)$min_dist)));
    
    //$dist_color = 255;
    
    $foreign = "";
    if (!strstr($item['Region'], 'Chile'))
      $foreign = "background:Orange;";
      
    echo "<tr class='item' onmouseover='setOrigin({$item['Lat']}, {$item['Lon']});'>";
    echo "  <td>".$counter."</td><td>&nbsp;|&nbsp;</td>".
         "<td>".date("D d. M, H:i:s", $item['Unix']-(4*60*60))." (.cl)</td>".
         "<td>&nbsp;|&nbsp;</td>".
         "<td>".date("D d. M, H:i:s", $item['Unix'])." (.de)</td>".
         "<td>&nbsp;|&nbsp;</td>".
         "<td style='background:rgb(255,$magn_color,$magn_color);'>".$item['Magnitude']."</td>".
         "<td>&nbsp;|&nbsp;</td>".
         "<td style='text-align:right;background:rgb(255,255,$dist_color);'>".$item['Distance']." km</td>".
         //"<td>&nbsp;|&nbsp;</td>".
         //"<td style='text-align:right;background:rgb(255,255,$dist_color);'>".round($item['Distance'] * 0.621371192)." mi</td>".
         "<td>&nbsp;|&nbsp;</td>".
         "<td style='$foreign'><a target='_blank' href='http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/".$item['Src'].$item['Eqid'].".php'>".$item['Region']."</a></td>";
    echo "</tr>";
    $counter++;
  }  
  
  echo "</table>";
  
  if ($fullpage == false)
    echo "      </div>\n";
  echo "    </td>\n";
  echo "  </tr>\n";
  echo "</table>";
  
  $min_magn -= 0.5;
  
  ###########################################################################################################################################################################
  
  function getStats($dataset, $min_magn, $min_unix, $max_unix) {
    
    $num = sizeof($dataset);

    echo "    data_chile = new google.visualization.DataTable();\n";
    echo "    data_chile.addColumn('datetime', 'Timestamp');\n";
    echo "    data_chile.addColumn('number', '0');\n";
    echo "    data_chile.addColumn('string', 'title1');\n";
    echo "    data_chile.addColumn('string', 'text1');\n";
    echo "    data_chile.addRows(". ( 3 * ( $num + 2 ) ).");\n";
    echo "    data_chile.setValue(0, 0, new Date(".(($min_unix)*1000)."));\n";
    echo "    data_chile.setValue(0, 1, $min_magn);\n";
    echo "    data_chile.setValue(1, 0, new Date(".(($min_unix)*1000)."));\n";
    echo "    data_chile.setValue(1, 1, $min_magn);\n";
    echo "    data_chile.setValue(2, 0, new Date(".(($min_unix)*1000)."));\n";
    echo "    data_chile.setValue(2, 1, $min_magn);\n";
    
    $i = 1;
    foreach($dataset as $item) {
      $unix = $item['Unix'];
      $magn = $item['Magnitude'];
      echo "    data_chile.setValue(".($i*3+0).", 0, new Date(".$unix."*1000-1000));\n";
      echo "    data_chile.setValue(".($i*3+0).", 1, $min_magn);\n";
      echo "    data_chile.setValue(".($i*3+1).", 0, new Date(".$unix."*1000));\n";
      echo "    data_chile.setValue(".($i*3+1).", 1, ".($magn).");\n";
      echo "    data_chile.setValue(".($i*3+2).", 0, new Date(".$unix."*1000+1000));\n";
      echo "    data_chile.setValue(".($i*3+2).", 1, $min_magn);\n";
      $i++;
    }
    
    echo "    data_chile.setValue(".($i*3+0).", 0, new Date(currentUnix));\n";
    echo "    data_chile.setValue(".($i*3+0).", 1, $min_magn);\n";
    echo "    data_chile.setValue(".($i*3+1).", 0, new Date(currentUnix));\n";
    echo "    data_chile.setValue(".($i*3+1).", 1, $min_magn);\n";
    echo "    data_chile.setValue(".($i*3+2).", 0, new Date(currentUnix));\n";
    echo "    data_chile.setValue(".($i*3+2).", 1, $min_magn);\n";
    
  }
  
  function drawStats() {
    echo "    chart_chile = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div_chile'));\n";
    echo "    chart_chile.draw(data_chile, {scaleType: 'maximized', isplayAnnotations: false, annotationsWidth: 0, thickness: 2, displayRangeSelector: true});\n";
  }
  
  if ($timeline == 'visible') {
    echo "<script type='text/javascript' src='http://www.google.com/jsapi'></script>\n";
    echo "<script type='text/javascript'>\n";
    echo "  var currentUnix = (new Date()).getTime();\n";
    echo "  var chart_chile;\n";
    echo "  var data_chile;\n";
    echo "  var google;\n";
    echo "  google.load('visualization', '1', {'packages':['annotatedtimeline']});\n";
    echo "  google.setOnLoadCallback(drawChart);\n";
    echo "  function drawChart() {\n";
    getStats($dataset, $min_magn, $min_unix, $max_unix);
    drawStats();
    echo "  }\n";
    echo "</script>\n";
  }

  echo "</body>";
  echo "</html>";
?>
